package cn.edu.xmu.orderpayment.payment.Dao;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.orderpayment.payment.mapper.ErrorAccountPoMapper;
import cn.edu.xmu.orderpayment.payment.model.bo.ErrorAccount;
import cn.edu.xmu.orderpayment.payment.model.po.ErrorAccountPo;
import cn.edu.xmu.orderpayment.payment.model.po.ErrorAccountPoExample;
import cn.edu.xmu.orderpayment.payment.model.vo.SimpleErrorAccount;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.*;

/**
 * @Author Zhang Liu
 **/
@Repository
public class ErrorAccountDao {

    @Autowired(required = false)
    private ErrorAccountPoMapper errorAccountPoMapper;
    private static final Logger logger = LoggerFactory.getLogger(ErrorAccountDao.class);

    public ReturnObject adminSelecterrorAccounts(Long patternId, String documentId, Byte state, LocalDateTime beginTime, LocalDateTime endTime, Integer page, Integer pageSize) {
        ErrorAccountPoExample example=new ErrorAccountPoExample();
        ErrorAccountPoExample.Criteria criteria=example.createCriteria();
//        ErrorAccount errorAccount=new ErrorAccount();
        if(patternId!=null)
            criteria.andPatternIdEqualTo(patternId);
        if(documentId!=null)
            criteria.andDocumentIdEqualTo(documentId);
        if(state!=null)
            criteria.andStateEqualTo(state);
        if(beginTime!=null)
            criteria.andTimeGreaterThanOrEqualTo(beginTime);
        if(endTime!=null)
            criteria.andTimeLessThanOrEqualTo(endTime);
        ReturnObject ret=selecterrorAccountsByExample(example, SimpleErrorAccount.class,page,pageSize);
        return ret;
    }

    public ReturnObject selecterrorAccountsByExample(ErrorAccountPoExample example,Class voClass,Integer page,Integer pageSize) {
        try{
            PageHelper.startPage(page,pageSize);
            List<ErrorAccountPo> pos= errorAccountPoMapper.selectByExample(example);
            PageInfo<ErrorAccountPo> errorAccountPoPageInfo=new PageInfo<>(pos);
            ReturnObject ret= new ReturnObject(errorAccountPoPageInfo);
            return Common.getPageRetVo(ret,voClass);
        }
        catch (Exception e){
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR,e.getMessage());
        }
    }
    public ReturnObject selecterrorAccountsByPrimaryKey(Long id) {
        try{
            ErrorAccountPo po=errorAccountPoMapper.selectByPrimaryKey(id);
            ReturnObject ret = new ReturnObject(po);
            return Common.getRetVo(ret, ErrorAccount.class);
        }
        catch (Exception e)
        {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR,e.getMessage());
        }
    }

    public ReturnObject updateErrorAccount(ErrorAccount bo, Long loginuser, String loginUserName) {
        try {
            ErrorAccountPo po=errorAccountPoMapper.selectByPrimaryKey(bo.getId());
            if(po == null)
                return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST,"错账id不存在");
            ErrorAccount retbo=(ErrorAccount)cloneVo(po,ErrorAccount.class);
//            if(!po.getState().equals(ErrorAccount.State.TOBEPAID.getCode().byteValue()))
//                return new ReturnObject(ReturnNo.STATENOTALLOW,"当前状态禁止此操作",retbo);
            po.setState(bo.getState());
            if(bo.getDescr()!=null)
                po.setDescr(bo.getDescr());
            setPoModifiedFields(po,loginuser,loginUserName);
            errorAccountPoMapper.updateByPrimaryKeySelective(po);
            return new ReturnObject(retbo);
        }
        catch (Exception e)
        {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR,e.getMessage());
        }
    }

//    public ReturnObject insertErrorAccount(ErrorAccount bo, Long loginuser, String loginUserName) {
//        try
//        {
//            ErrorAccountPo po=(ErrorAccountPo)cloneVo(bo,ErrorAccountPo.class);
//            setPoCreatedFields(po,loginuser,loginUserName);
//            errorAccountPoMapper.insert(po);
//            return new ReturnObject(cloneVo(po,ErrorAccount.class));
//        }
//        catch (Exception e)
//        {
//            logger.error(e.getMessage());
//            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR,e.getMessage());
//        }
//    }

}
